import EventManager from "../../Common/EventManager";
import GlobalPool from "../../Common/GlobalPool";
import Loader from "../../Common/Loader";
import yyComponent from "../../Common/yyComponent";
import UserService from "../../frameSccript/services/userService";
import { mgrs, sers } from "../../frameSccript/system/OverallSystem ";
import { EventType } from "../../GameSpecial/GameEventType";

const {ccclass, property} = cc._decorator;

@ccclass
export default class gameItem extends yyComponent {

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    @property(Number)
    public LevelId: number = 1;

    @property(cc.Node)
    private baokuang:cc.Node = null

    public bao = false

    @property(cc.Node)
    private tongguan:cc.Node = null

    public gameId: number = 1;
    public title: string ='';

    public data= null

    public Id_:number = 0;

    public unlockNumber = 1
    public muqianguankancishu = 0

    @property(cc.Node)
    parentNode:cc.Node = null

    @property(cc.Material)
    gray:cc.Material = null;

    @property(cc.Material)
    gray2:cc.Material = null;

    private unlock = false
    start () {
        // this.node.on(cc.Node.EventType.TOUCH_CANCEL,this.touchItem,this)
        this.on(EventType.DirectorEvent.updataLevelList, this.updateView, this)
        
    }

    touchItem(){
        // if(!this.node.getComponent(cc.Button).interactable){
            cc.log('视频解锁')
            let data = {
                level:this.LevelId,
                gameId:this.gameId,
                title:this.title,
                unlockNumber:this.unlockNumber,
                muqianguankancishu:this.muqianguankancishu,
                muqianItem:this
            }
            this.emit(EventType.DirectorEvent.lockLevel,data)
        // }
    }
    @property(cc.Node)
    xin:cc.Node = null

    public updateView(){
        this.baokuang.active=false
        this.tongguan.active=false
        let data = sers().userSrv.getGameLevelData()

        this.baokuang.active = this.bao
        this.xin.active = this.data.xin
        
        
        // this.node.getComponent(cc.Button).interactable = false
        this.unlock = false
        for (const lock of data.lock) {
            if(lock.gameId == this.gameId && lock.level == this.LevelId){
                // this.node.getComponent(cc.Button).interactable = true
                this.unlock = true
            }
        }

        for (const lock of data.pass) {
            if(lock.gameId == this.gameId && lock.level == this.LevelId){
                this.tongguan.active = true
                this.unlock = true
            }
        }

        // if(this.LevelId==1 && this.gameId==1){
        //     // this.node.getComponent(cc.Button).interactable = true   
        //     this.unlock = true
        // }

        if(this.Id_== 0){
            this.unlock = true
        }


        if(Number(sers().localStorageSrv.get(UserService.infinityPowerModel)) != 0){
            this.unlock = true
        }

        if(this.gameId==3){
            Loader.loadBundleRes('NormalMode','image/zi/'+this.data.zi,(res)=>{
                if(!this || !this.node)return
                this.node.getChildByName('zi').getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(res)
            })
        }

        if (cc.sys.platform == cc.sys.ANDROID){
            this.unlock = true
        }

        this.node.getComponent(cc.Sprite).setMaterial(0,this.unlock ? this.gray : this.gray2)
    }

    startGame(){
        mgrs().soundMgr.playSound('click');

        console.log(this.Id_)

        if(!this.unlock){
            this.touchItem()
        }else{
            if(this.LevelId>0){
                this.loadLevelScene()  
            }
        }

    }

    protected loadLevelScene() {
        sers().uiSrv.showLoding();
        let str = ''
        if(this.gameId>1){
            str = this.gameId+''
        }
        // console.log('加载LevelScene'+str)
        Loader.loadBundle("LevelScene"+str, (res) => {
            Loader.loadBundleDir("LevelScene"+str, "SinglePrefab", (res) => {
                for (let i = res.length - 1; i >= 0; --i) {
                    let r = res[i];
                    GlobalPool.createPool(r.name, r, r.name);
                }
                sers().userSrv.setLevel(this.LevelId)
                sers().userSrv.setGuanqiaID(this.Id_)
                sers().userSrv.setGameId(this.gameId)
                sers().userSrv.setGuanqiaData(this.data)
                // let game = GlobalPool.get('gameScene1Level'+this.LevelId)
                // this.parentNode.addChild(game)
                // game.x = game.y = 0
                let game = GlobalPool.get('gameScene'+this.gameId)
                this.parentNode.addChild(game)
                game.x = game.y = 0
                if(this.gameId!=1){
                    sers().uiSrv.hideLoding();
                }

            }, cc.Prefab, true);
        }, true, true);

        sers().platformSrv.platformApi.FullScreenVideoAd()
    }

    // update (dt) {}
}
